<template>
  <el-card shadow="never">
    <!-- 新增刷新 -->
    <ListHeader @create="handleCreate" @refresh="getData"></ListHeader>

    <!-- 表格 -->

    <el-table :data="tableData" style="width: 100%" v-loading="loading">
      <el-table-column label="管理员">
        <template #default="{ row }">
          {{ row.admin.username }}
        </template>
      </el-table-column>
      <el-table-column label="產品名稱" :show-overflow-tooltip="true">
        <template #default="{ row }">
          {{ row.product ? row.product.name :''}}
        </template>
      </el-table-column>
      <el-table-column prop="num" label="採購数量" />
      <el-table-column prop="supplier" label="供應商" />
      <el-table-column prop="connection" label="聯繫方式" />
      <el-table-column prop="money" label="金額" />
      <el-table-column prop="desc" label="備註" />
      <el-table-column prop="cost" label="成本" />
      <el-table-column prop="created_at" label="採購時間" />
      <el-table-column label="操作" width="280">
        <template #default="{ row }">
          <el-button size="small"  type="primary" @click="handleEdit(row)">修改</el-button>
          <el-button size="small" :disabled="row.type==1" type="success" @click="handleConfirm(row)">收貨</el-button> 
          <el-popconfirm
            title="是否要删除?"
            confirm-button-text="確定"
            cancel-button-text="取消"
            @confirm="handleDelete(row.id)"
          >
            <template #reference>
              <el-button  type="danger" size="small">删除</el-button>
            </template>
            
                
          </el-popconfirm>
          <el-button size="small"  :disabled="row.status==1" type="success" @click="handleMail(row)">{{ row.butt }}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      background
      layout="prev, pager, next"
      :total="total"
      class="flex justify-center mt-4"
      @current-change="getData"
      :current-page="curpage"
      :page-size="limit"/>

    <!-- 提交表单 -->
    <FormDrawer ref="formDrawerRef" @submit="handleSubmit" :title="drawerTitle">
      <el-form :model="form" ref="formRef" :rules="rules" label-width="auto">
        <el-form-item label="產品名稱" prop="product_id">
          <el-select v-model="form.product_id" placeholder="請選擇">
            <el-option
              v-for="item in products"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="採購数量" prop="num">
          <el-input-number v-model="form.num" :min="1" :max="1000" />
        </el-form-item>
        <el-form-item label="供應商" prop="supplier">
          <el-input v-model="form.supplier"></el-input>
        </el-form-item>
        <el-form-item label="聯繫方式" prop="connection">
          <el-input v-model="form.connection"></el-input>
        </el-form-item>
        <el-form-item label="金額" prop="money">
          <el-input v-model="form.money"></el-input>
        </el-form-item>
        <el-form-item label="成本" prop="cost">
          <el-input v-model="form.cost"></el-input>
        </el-form-item>
        <el-form-item label="備註" prop="desc">
          <el-input v-model="form.desc" type="textarea"></el-input>
        </el-form-item>
      </el-form>
    </FormDrawer>
  </el-card>
</template>

<script setup>
import { ref } from "vue";

import {
  getPurchase,
  createPurchase,
  updatePurchase,
  deletePurchase,
  mail,signfor
} from "@/api/stockpile.js";

import FormDrawer from "@/components/FormDrawer.vue";
import ListHeader from "@/components/ListHeader.vue";
import { msg } from "@/composables/util"
import { useInitTable, useInitForm } from "@/composables/useCommon.js";
const products = ref(null);

const {
  tableData,
  loading,
  curpage,
  total,
  limit,
  getData,
  handleDelete,
} = useInitTable({
  getList: getPurchase,
  delete: deletePurchase,
  onGetListSuccess: (res) => {
    
    products.value = res.product;
   
    (total.value = res.data.total), (tableData.value = res.data.data);
    tableData.value.forEach(item => {
    item.butt = item.status == 1 ? '已發送' : '發送郵件';
  });
  },
});


//表单

const {
  formDrawerRef,
  formRef,
  form,
  rules,
  drawerTitle,
  handleSubmit,
  handleCreate,
  handleEdit,
} = useInitForm({
  form: {
    product_id: "",
    num: 1,
    supplier: "",
    connection: "",
    desc: "",
    money: 0,
  },
  rules: {
    connection: [{ required: true, message: "郵箱不能為空", trigger: "blur" }],
    product_id: [{ required: true, message: "產品不能為空", trigger: "blur" }],
  },
  getData,
  update: updatePurchase,
  create: createPurchase,
});

const handleMail=(row)=>{
   mail(row).then(res=>{
    getData()
    msg('郵件發送成功')
   })
}

const handleConfirm=(row)=>{
  signfor(row.id,row).then(res=>{
    getData()
    msg('確認收貨')
  })
}
</script>
